﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class ChanPin_Out_Bill : System.Web.UI.Page
    {

        protected string selectid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            if (!this.IsPostBack)
            {
                Button_Query_Click(null, null);
            }
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChanPin_Out.aspx");
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.DanHao.Text.Trim().Replace("'", "");
            string Sql = "";
            if (temp != "")
            {
                Sql = "[序号] like '%" + temp + "'";
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                if (Sql != "")
                {
                    Sql += " and ";
                }
                Sql += "[发运时间]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                if (Sql != "")
                {
                    Sql += " and ";
                }
                Sql += "[发运时间]<='" + Dt.ToShortDateString() + "'";
            }
            this.SqlDataSource1.SelectCommand = "";
            if (Sql == "")
            {
                this.SqlDataSource1.SelectCommand = "Select [ID],序号,合同编号,品名,发往单位,整车捆数,整车吨位,发运时间,是否合格,是否送检,~是否合格 as [锁定] from [产品出库清单]";
            }
            else
            {
                this.SqlDataSource1.SelectCommand = "Select [ID],序号,合同编号,品名,发往单位,整车捆数,整车吨位,发运时间,是否合格,是否送检,~是否合格 as [锁定] from [产品出库清单] where " + Sql + " order by [发运时间] desc";
            }
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
            GridView1.SelectedIndex = -1;
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("ChanPin_Out.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec ChanPin_Out_Bill_Delete " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                if (this.SqlDataSource1.Delete() > 0)
                {
                    this.Message.Text = "·删除成功";
                }
                else
                {
                    this.Message.Text = "·删除失败";
                }

                this.GridView1.DataBind();
                
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex > -1)
            {
                selectid = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            int k=0;
            if(((CheckBox)sender).Checked)k=1;
            this.SqlDataSource1.UpdateCommand = "update 产品出库单 set 是否送检=" + k + " where id=" + ((CheckBox)sender).Text;
            this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;
        }

    }
}
